/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package quanlysieuthi;
import DAO.*;
import DTO.*;
import java.util.ArrayList;
import java.util.List;
import Model.*;
//import com.sun.javafx.scene.control.skin.VirtualFlow;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
/**
 *
 * @author quangphuc
 */
public class FrmKhuyenMai extends javax.swing.JFrame implements ActionListener{

    /**
     * Creates new form FrmKhuyenMai
     */
    public FrmKhuyenMai() {
        initComponents();
        this.dtctu_ngay.setDateFormatString(QuanLySieuThi.formatDate); 
        this.dtcden_ngay.setDateFormatString(QuanLySieuThi.formatDate);
        loadData();
        setListTable();
        //eventInit();
    }
    Boolean edit = false;
    SubConfig config = null;
    List<CKhuyen_mai> lkh_mai = new ArrayList<CKhuyen_mai>();
    CKhuyenMaiDAO kmDao = new CKhuyenMaiDAO();
    CLoai_KhuyenMaiDAO lkmdao = new CLoai_KhuyenMaiDAO();
    List<CLoai_KhuyenMai> listLKM = new ArrayList<CLoai_KhuyenMai>();
    List<CSanPham> listSanPham = new ArrayList<CSanPham>();
    CSanPhamDAO spdao = new CSanPhamDAO();
    List<ChiTietKhuyenMai> list_chi_tiet_km = new ArrayList<ChiTietKhuyenMai>();
    JPopupMenu popupMenu;
    public int m_km_id = -1;
    private JMenuItem itemDelete;
    private JMenuItem itemKhuyemMai;
    private int index = 0;
    private int row = 0;
    CKhuyen_mai currentKm = null;
    public void reLoadData(){
        loadData();
        setListTable();
    }
    
    private void eventInit(){
        popupMenu = new JPopupMenu();
        itemDelete = new JMenuItem("Xoa");
        itemDelete.addActionListener(this);
        popupMenu.add(itemDelete);//itemKhuyemMai
        
        itemKhuyemMai = new JMenuItem("Them san pham khuyen mai");
        itemKhuyemMai.addActionListener(this);
        popupMenu.add(itemKhuyemMai);//itemKhuyemMai
        this.tbldanh_sachsp.setComponentPopupMenu(popupMenu);
    }
    private void showForm(){        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JMenuItem menu = (JMenuItem) e.getSource();
        if(menu == itemDelete){
            deleteItemProduct();
        }
        else if(menu == itemKhuyemMai){
            showForm();
        }
    }
    private void deleteItemProduct(){
        int rowIndex = this.tbldanh_sachsp.getSelectedRow();
        if(rowIndex >-1 && rowIndex < this.list_chi_tiet_km.size()){
           // String path = tbldanh_sachsp.getModel().getValueAt(rowIndex, 1).toString();
            ChiTietKhuyenMai ctkm = this.list_chi_tiet_km.get(rowIndex);
            if(JOptionPane.showConfirmDialog(this, ctkm.getMaHangMua()) == JOptionPane.YES_OPTION){
                //tbldanh_sachsp.remove(rowIndex);
                this.list_chi_tiet_km.remove(rowIndex);
               // tbldanh_sachsp.removeRowSelectionInterval(rowIndex, rowIndex);
                setListTable();
            }         
        }
    }
    public void setChiTietKm(List<ChiTietKhuyenMai> list){
        this.list_chi_tiet_km = list;
        setListTable();
    }
    
    private void setListTable(){        
        Vector<Vector> data = new Vector<Vector>();
        for(ChiTietKhuyenMai item : list_chi_tiet_km){
            Vector<String> row = new Vector<String>();
            //row.addElement("" + item.getKmId());
            row.addElement("" + item.getMaHangMua());
            row.addElement("" + item.getSoLuongMua());
            row.addElement("" + item.getMaHangTang());
            row.addElement("" + item.getSoLuongTang());
            String strGiamgia = "";
            if(item.getGiamgia() > 0){
                strGiamgia = "" + item.getGiamgia() + "%";
            }
            row.addElement(strGiamgia);
            data.add(row);
        }
        Vector<String> columnNames = new Vector<String>();
       // columnNames.addElement("ID");
        columnNames.addElement("Sẩn phẩm mua");
        columnNames.addElement("Số lượng mua");
        columnNames.addElement("Sản phẩm tặng");
        columnNames.addElement("Số lượng tặng");
        columnNames.addElement("Giảm giá(%)");
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        this.tbldanh_sachsp.removeAll();
        this.tbldanh_sachsp.setModel(model);
        this.tbldanh_sachsp.setFillsViewportHeight(true);            
    }
    
    private void loadData(){
        Date d = new Date();        
        this.dtctu_ngay.setDate(d);
        this.dtcden_ngay.setDate(d);        
        config = SubConfig.getInstance();
        config.init();
        listLKM = lkmdao.getAll(config);
        lkh_mai = kmDao.getAll(config);
        listSanPham = spdao.getAll(config);     
        //lkh_mai =
        lkh_mai = kmDao.getAll(config);
        row = lkh_mai.size();        
        config.close();      
        if(row > 0){            
            this.setContents(index);
        }
        else
            txtrow.setText("0/0");      
        setEditTable(false);
        this.btnaddNew.setEnabled(true);
    }
    
    private void setContents(int i){
        currentKm = null;
        if(i>=0 && i < row){
            CKhuyen_mai km = lkh_mai.get(i);
            currentKm = km;
            this.txtTenCTKM.setText(km.getLoaiKm());
            this.dtcden_ngay.setDate(km.getDenNgay());
            this.dtctu_ngay.setDate(km.getTuNgay());
            this.chkap_dung.setState(km.getApDung());
            this.txtmo_ta.setText(km.getMota());
            txtrow.setText(String.format("%s/%s", i + 1,row));
            getListSanPhamChiTiet(km.getMatHangMua(),km.getMatHangTang(),km.getGiamGia());
            setListTable();
        }
    }
    
    private void updateSoluong(List<CSanPham> list,int []a){
        for(int i = 0;i < list.size();i++){
            CSanPham item = list.get(i);
            item.setSoLgTonToiDa(a[i]);
        }
    }
    
    private void getListSanPhamChiTiet(String mua_hang_id,String tang_hang_id,double giam_gia){
        List<CSanPham> mua_hang = new ArrayList<CSanPham>();
        List<CSanPham> tang_hang = new ArrayList<CSanPham>();
        config = SubConfig.getInstance();
        config.init();
        String []strMuahangid = mua_hang_id.split(",");
        String mh_id = "";
        int []a_mh = new int[strMuahangid.length];
        String m ="";
        for(int i = 0; i < strMuahangid.length;i++){
            m = strMuahangid[i];
            String []am = m.split("::");
            if(!mh_id.equals("")){
                mh_id = mh_id + ",";
            }
            mh_id = mh_id + am[0];
            if(am.length >=2){
                int t = Integer.parseInt(am[1]);
                a_mh[i] = t;
            }
        }
        String tang_id = "";
        strMuahangid = tang_hang_id.split(",");
        int []b_mh = new int[strMuahangid.length];
        m = "";
        for(int i = 0; i < strMuahangid.length;i++){
            m = strMuahangid[i];
            String []am = m.split("::");
            if(!tang_id.equals("")){
                tang_id = tang_id + ",";
            }
            tang_id = tang_id + am[0];
            if(am.length >=2){
                int t = Integer.parseInt(am[1]);
                b_mh[i] = t;
            }
        }
        
        String mysql = "select * from " + CFields.tb_san_pham + " where id in(";
        mua_hang = spdao.getAll(mysql + mh_id + ")", config);
        tang_hang = spdao.getAll(mysql + tang_id + ")", config);
        this.updateSoluong(mua_hang, a_mh);
        this.updateSoluong(tang_hang, b_mh);
        list_chi_tiet_km = new ArrayList<ChiTietKhuyenMai>();
        int i = 0;
        int l = 0;
        ChiTietKhuyenMai ct;
        CSanPham item ;
        if(mua_hang.size() > tang_hang.size()){
            l = tang_hang.size();
            for(i = 0; i < l;i++){
                ct = new ChiTietKhuyenMai();
                item = mua_hang.get(i);
                ct.setMhmId(item.getId());
                ct.setMaHangMua(item.getTenSanPham());
                ct.setSoLuongMua(item.getSoLgTonToiDa());
                item = tang_hang.get(i);
                ct.setMhtId(item.getId());
                ct.setMaHangTang(item.getTenSanPham());
                ct.setGiamGia(giam_gia);
                ct.setSoLuongTang(item.getSoLgTonToiDa());
                if(this.list_chi_tiet_km.contains(ct)== false){
                    this.list_chi_tiet_km.add(ct);
                }
            }
            while(i < mua_hang.size()){
                ct = new ChiTietKhuyenMai();
                item = mua_hang.get(i);
                ct.setMhmId(item.getId());
                ct.setMaHangMua(item.getTenSanPham());                
                ct.setGiamGia(giam_gia);
                ct.setSoLuongMua(item.getSoLgTonToiDa());
                if(this.list_chi_tiet_km.contains(ct)== false){
                    this.list_chi_tiet_km.add(ct);
                }
                i++;
            }
        }
        else{
            l = mua_hang.size();
            for(i = 0; i < l;i++){
                ct = new ChiTietKhuyenMai();
                item = mua_hang.get(i);
                ct.setMhmId(item.getId());
                ct.setMaHangMua(item.getTenSanPham());
                ct.setSoLuongMua(item.getSoLgTonToiDa());
                item = tang_hang.get(i);
                ct.setMhtId(item.getId());
                ct.setMaHangTang(item.getTenSanPham());
                ct.setSoLuongTang(item.getSoLgTonToiDa());
                ct.setGiamGia(giam_gia);
                if(this.list_chi_tiet_km.contains(ct)== false){
                    this.list_chi_tiet_km.add(ct);
                }
            }
            while(i < tang_hang.size()){
                ct = new ChiTietKhuyenMai();
                item = tang_hang.get(i);
                ct.setMhmId(item.getId());
                //ct.setMaHangMua(item.getTenSanPham());    
                ct.setMaHangTang(item.getTenSanPham());
                ct.setSoLuongTang(item.getSoLgTonToiDa());
                ct.setGiamGia(giam_gia);
                if(this.list_chi_tiet_km.contains(ct)== false){
                    this.list_chi_tiet_km.add(ct);
                }
                i++;
            }
        }
        config.close();
                
    }
    
    private void addChiTiet(){        
        
    }
    
    private void addNew(){
        String mat_hang_mua ="";
        String mat_hang_tang ="";
        double giam_gia =0.0;
        for(ChiTietKhuyenMai item : list_chi_tiet_km){
            String strMua = "";
            String strTang = "";
            if(item.getMhmId()!=-1){
                strMua = "" + item.getMhmId();
                if(mat_hang_mua.equals("") == false){
                    mat_hang_mua = mat_hang_mua + ",";
                }
                mat_hang_mua = mat_hang_mua + strMua;
            }
            if(item.getMhtId()!=-1){
                strTang = "" + item.getMhtId();
                if(mat_hang_tang.equals("") == false){
                    mat_hang_tang = mat_hang_tang + ",";
                }
                mat_hang_tang = mat_hang_tang + strTang;
            }
            giam_gia = item.getGiamgia();
        }
        
        int km_id = m_km_id;
        Date d = this.dtctu_ngay.getDate();
        Timestamp tu_ngay = new Timestamp(d.getYear(), d.getMonth(), d.getDay(), d.getHours(), d.getMinutes(), d.getSeconds(), 0);
        d = this.dtcden_ngay.getDate();
        Timestamp den_ngay = new Timestamp(d.getYear(), d.getMonth(), d.getDay(), d.getHours(), d.getMinutes(), d.getSeconds(), 0);
        //new Timestamp(d.getYear(),d.getMonth(),d.getDay(),d.getHours(),d.getMinutes(),d.getSeconds(),0);
        Boolean ap_dung = this.chkap_dung.getState();
        Boolean kh_ap_dung = true;
        String mo_ta ="";
        String km = this.txtTenCTKM.getText();
        CModel _model = new CModel(QuanLySieuThi.user_name);
        _model.insertKhuyenMai(km, tu_ngay, den_ngay, ap_dung, mat_hang_mua, mat_hang_tang, giam_gia, kh_ap_dung, mo_ta, km_id);
        _model.saveHisTory();
        _model.Close();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTenChuongTrinhKhuyenMai = new javax.swing.JLabel();
        txtTenCTKM = new javax.swing.JTextField();
        lblTuNgay = new javax.swing.JLabel();
        lblDenNgay = new javax.swing.JLabel();
        chkap_dung = new java.awt.Checkbox();
        lblDanhSachCacSanPham = new javax.swing.JLabel();
        label1 = new java.awt.Label();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbldanh_sachsp = new javax.swing.JTable();
        btnSua = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtmo_ta = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        btnPrevBegin = new javax.swing.JButton();
        txtrow = new javax.swing.JTextField();
        btnNextEnd = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        dtctu_ngay = new com.toedter.calendar.JDateChooser();
        dtcden_ngay = new com.toedter.calendar.JDateChooser();
        btnkhuyenmai = new javax.swing.JButton();
        lbltieu_de = new javax.swing.JLabel();
        btnaddNew = new javax.swing.JButton();

        lblTenChuongTrinhKhuyenMai.setText("Tên chương trình khuyến mãi:");

        lblTuNgay.setText("Từ ngày");

        lblDenNgay.setText("Đến ngày");

        chkap_dung.setLabel("Áp dụng");

        lblDanhSachCacSanPham.setText("Danh sách các sản phẩm khuyến mãi");

        label1.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
        label1.setText("Loại chương trình áp dụng");

        tbldanh_sachsp.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbldanh_sachsp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbldanh_sachspMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbldanh_sachsp);

        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");

        jLabel1.setText("Ghi chú:");

        txtmo_ta.setColumns(20);
        txtmo_ta.setRows(5);
        txtmo_ta.setText("sss\n");
        jScrollPane2.setViewportView(txtmo_ta);

        jButton1.setText("<");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btnPrevBegin.setText("<<");
        btnPrevBegin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevBeginActionPerformed(evt);
            }
        });

        txtrow.setText("1/10");

        btnNextEnd.setText(">>");
        btnNextEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextEndActionPerformed(evt);
            }
        });

        jButton4.setText(">");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        dtctu_ngay.setDateFormatString("dd/MM/yyyy");

        dtcden_ngay.setDateFormatString("dd/MM/yyyy");

        btnkhuyenmai.setText("Thêm khuyến mãi");
        btnkhuyenmai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnkhuyenmaiActionPerformed(evt);
            }
        });

        lbltieu_de.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lbltieu_de.setForeground(new java.awt.Color(102, 102, 255));
        lbltieu_de.setText("QUẢN LÝ CHƯƠNG TRÌNH KHUYẾN MÃI");

        btnaddNew.setText("Thêm mới");
        btnaddNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaddNewActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 457, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(45, 45, 45)
                        .addComponent(btnkhuyenmai))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(24, 24, 24)
                                .addComponent(lblTuNgay)
                                .addGap(18, 18, 18)
                                .addComponent(dtctu_ngay, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(58, 58, 58)
                                .addComponent(lblDenNgay)
                                .addGap(18, 18, 18)
                                .addComponent(dtcden_ngay, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblTenChuongTrinhKhuyenMai)
                                .addGap(18, 18, 18)
                                .addComponent(txtTenCTKM, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addComponent(chkap_dung, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(222, 222, 222))
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(lblDanhSachCacSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(btnaddNew)
                        .addGap(31, 31, 31)
                        .addComponent(btnSua)
                        .addGap(18, 18, 18)
                        .addComponent(btnThoat)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnPrevBegin)
                        .addGap(24, 24, 24)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(txtrow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton4)
                        .addGap(18, 18, 18)
                        .addComponent(btnNextEnd)
                        .addGap(17, 17, 17)))
                .addGap(37, 37, 37))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(lbltieu_de)
                .addGap(305, 305, 305))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbltieu_de)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblTenChuongTrinhKhuyenMai)
                        .addComponent(txtTenCTKM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(chkap_dung, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(dtcden_ngay, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(dtctu_ngay, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(label1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblTuNgay, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblDenNgay, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(68, 68, 68))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(36, 36, 36)
                                .addComponent(btnkhuyenmai))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addComponent(lblDanhSachCacSanPham)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnSua)
                        .addComponent(btnThoat)
                        .addComponent(btnaddNew))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnPrevBegin)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtrow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton4)
                            .addComponent(jButton1)
                            .addComponent(btnNextEnd))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbldanh_sachspMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbldanh_sachspMouseClicked
        // TODO add your handling code here:
        JTable table =(JTable)evt.getSource();
        Point p = evt.getPoint();
        int rowIndex = table.rowAtPoint(p);        
        if(rowIndex >-1){
            table.setRowSelectionInterval(rowIndex, rowIndex);
        }
    }//GEN-LAST:event_tbldanh_sachspMouseClicked

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        if(index < row - 1){
            index = index + 1;
            this.setContents(index);            
        }
        else{
            return;
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
         if(index > 0){
            index = index - 1;
            this.setContents(index);
        }
        else{
            return;
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnPrevBeginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevBeginActionPerformed
        // TODO add your handling code here:
        index = 0;
        this.setContents(index);            
    }//GEN-LAST:event_btnPrevBeginActionPerformed

    private void btnNextEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextEndActionPerformed
        // TODO add your handling code here:
        index = row -1;
        this.setContents(index);
    }//GEN-LAST:event_btnNextEndActionPerformed

    private void btnkhuyenmaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnkhuyenmaiActionPerformed
        // TODO add your handling code here:        
        HashMap khuyenMai = new HashMap();
        khuyenMai.put("tenct",this.txtTenCTKM.getText());
        khuyenMai.put("tungay", dtctu_ngay.getDate());
        khuyenMai.put("denngay", dtcden_ngay.getDate());
        khuyenMai.put("mota", txtmo_ta.getText());
        FrmChiTietKM f = new FrmChiTietKM(this, khuyenMai);
        f.setVisible(true);
    }//GEN-LAST:event_btnkhuyenmaiActionPerformed

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        // TODO add your handling code here:
        
        if(edit == false){
            edit = true;
            this.btnSua.setText("Lưu");
            this.txtTenCTKM.setEditable(true);
            this.txtmo_ta.setEditable(true);
        }
        else{
            edit = false;
            this.btnSua.setText("Sửa");
            this.currentKm.setLoaiKm(this.txtTenCTKM.getText());
            this.currentKm.setMoTa(txtmo_ta.getText());
            CModel _model = new CModel(QuanLySieuThi.user_name);
            if(_model.updateKhuyenMai(currentKm)){
                JOptionPane.showMessageDialog(this, "Cập nhật thành công");
            }
            this.loadData();
        }
    }//GEN-LAST:event_btnSuaActionPerformed
    
    private void setEditTable(Boolean b){
        this.txtTenCTKM.setEditable(b);
        this.txtmo_ta.setEditable(b);
        this.dtctu_ngay.setEnabled(b);
        this.dtcden_ngay.setEnabled(b);
        btnkhuyenmai.setEnabled(b);
    }
    
    private void btnaddNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnaddNewActionPerformed
        // TODO add your handling code here:
        setEditTable(true);
        this.btnaddNew.setEnabled(false);
    }//GEN-LAST:event_btnaddNewActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmKhuyenMai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmKhuyenMai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmKhuyenMai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmKhuyenMai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmKhuyenMai().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNextEnd;
    private javax.swing.JButton btnPrevBegin;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnaddNew;
    private javax.swing.JButton btnkhuyenmai;
    private java.awt.Checkbox chkap_dung;
    private com.toedter.calendar.JDateChooser dtcden_ngay;
    private com.toedter.calendar.JDateChooser dtctu_ngay;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private java.awt.Label label1;
    private javax.swing.JLabel lblDanhSachCacSanPham;
    private javax.swing.JLabel lblDenNgay;
    private javax.swing.JLabel lblTenChuongTrinhKhuyenMai;
    private javax.swing.JLabel lblTuNgay;
    private javax.swing.JLabel lbltieu_de;
    private javax.swing.JTable tbldanh_sachsp;
    private javax.swing.JTextField txtTenCTKM;
    private javax.swing.JTextArea txtmo_ta;
    private javax.swing.JTextField txtrow;
    // End of variables declaration//GEN-END:variables

  
}
